const log = require('./logger');
const got = require('got');
const cache = new Map();
/**
 * Download the album cover of a track.
 *
 * @param {Object} trackInfos
 */
const downloadAlbumCover = async (trackInfos) => {
  if (!trackInfos.ALB_PICTURE) return null;

  try {
    const { body, fromCache } = await got(
      'http://e-cdn-images.deezer.com/images/cover/' + trackInfos.ALB_PICTURE + '/1000x1000-000000-80-0-0.jpg',
      { encoding: null, cache }
    );
    log.debug(`Fetched cover for ${trackInfos.SNG_TITLE}, cache: ${fromCache}`);
    return body;
  } catch (err) {
    log.debug(`Failed to download cover for ${trackInfos.SNG_TITLE}, #${trackInfos.SNG_ID} Error: ${err}`);
    return null;
  }
};

module.exports = downloadAlbumCover;
